package controllers

import (
	"encoding/json"
	_ "gitchina/kly_service/errors"
	"gitchina/kly_service/models"
	"gitchina/kly_service/util"
	"github.com/astaxie/beego"
)

type OrderController struct {
	beego.Controller
}

// @router /103001 [post]
func (o *OrderController) MakeWaybill() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		id, err := ord.MakeWaybill(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(id)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103002 [post]
func (o *OrderController) UpdateWaybill() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		err := ord.UpdateWaybill(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(nil)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103003 [post]
func (o *OrderController) GetWaybillList() {
	var rq models.GetWaybillListRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rs, err := ord.GetWaybillList(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetWaybillList] - debug:%v", rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103005 [post]
func (o *OrderController) GetWaybillDetail() {
	var rq models.GetWaybillDetailRQ
	var ord models.Order
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		rs, err := ord.GetWaybillDetail(rq.Id)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103006 [post]
func (o *OrderController) UpdateWaybillState() {
	var rq models.UpdateWaybillStateWithParamsRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		ord.UpdateWaybillState(mbrId, &rq)
		o.Data["json"] = models.NewOkResponse(nil)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103007 [post]
func (o *OrderController) ViewContract() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		contract := ord.ViewContract(mbrId, &rq)
		o.Data["json"] = models.NewOkResponse(contract)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103008 [post]
func (o *OrderController) MakeLinebill() {
	var rq models.Linebill
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		id, err := ord.MakeLinebill(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(id)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103009 [post]
func (o *OrderController) GetLinebillList() {
	var rq models.GetLinebillListRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq)
	if err == nil {
		rs, err := ord.GetLinebillList(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		util.GetLogger().Debug("[ctrl-GetLinebillList] - debug:%v", rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103010 [post]
func (o *OrderController) GetLinebillDetail() {
	var rq models.GetLinebillDetailRQ
	var ord models.Order
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		rs, err := ord.GetLinebillDetail(rq.Id)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(rs)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103011 [post]
func (o *OrderController) UpdateLinebillState() {
	var rq models.UpdateLinebillStateRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		err := ord.UpdateLinebillState(mbrId, &rq)
		if err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(nil)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /103012 [post]
func (o *OrderController) ViewLetterOfAuthorization() {
	var rq models.MakeWaybillRQ
	var ord models.Order
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		contract := ord.ViewLetterOfAuthorization(mbrId, rq.Id)
		o.Data["json"] = models.NewOkResponse(contract)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}
